package com.silverhaze.diamond.client.ui.dialogs;

import com.silverhaze.diamond.common.entity.Customer;
import com.silverhaze.diamond.common.entity.Detail;
import com.silverhaze.diamond.common.entity.SystemUser;
import com.silverhaze.diamond.common.entity.Service;
import com.silverhaze.diamond.client.settings.SettingsStorage;
import com.silverhaze.diamond.client.settings.ui.MainFrameSettings;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.table.ServiceTable;
import com.silverhaze.diamond.client.ui.table.model.ServiceTableModel;
import com.silverhaze.commons.db.datasource.ObjectEntity;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * SystemUser: Babanin
 * Date: 24.07.2008
 * Time: 16:16:45
 */
public class SpecificServicesDialog extends AbstractDialog {
    public SpecificServicesDialog(List<Service> services, ObjectEntity entity) {
        super(UIHelper.getMainFrame());

        initialize(services, entity);
    }

    private void initialize(List<Service> services, ObjectEntity entity) {
        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel(new BorderLayout());

        if (entity instanceof Customer) {
            Customer c = (Customer) entity;
            setTitle("Сервисы данного покупателя");

            topPanel.add(new JLabel("<html><b>Покупатель:</b> " + c.getExtendedName() + "</html>"), BorderLayout.LINE_START);
        } else if (entity instanceof Detail) {
            Detail d = (Detail) entity;
            setTitle("Сервисы в которых использовалась данная деталь");

            topPanel.add(new JLabel("<html><b>Деталь:</b> " + d.getName() + "</html>"), BorderLayout.LINE_START);
        } else if (entity instanceof SystemUser) {
            SystemUser u = (SystemUser) entity;
            setTitle("Сервисы данного полльзователя");

            topPanel.add(new JLabel("<html><b>Пользователь:</b> " + u.getLogin() + "</html>"), BorderLayout.LINE_START);
        }

        topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel centerPanel = new JPanel(new BorderLayout());

        ServiceTableModel tableModel = new ServiceTableModel(services);
        ServiceTable table = new ServiceTable(tableModel);

        centerPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        add(topPanel, BorderLayout.PAGE_START);
        add(centerPanel, BorderLayout.CENTER);
        add(initializeOkCancelPanel(DialogType.VIEW), BorderLayout.PAGE_END);

        MainFrameSettings mainFrameSettings = SettingsStorage.getSettings().getUiSettings().getMainFrameSettings();
        setPreferredSize(new Dimension(mainFrameSettings.getWidth() - 100, 300));

        pack();
        setLocationRelativeTo(null);
    }

    protected void okPressed() {
        dispose();
    }

    public static void view(List<Service> services, ObjectEntity entity) {
        SpecificServicesDialog dialog = new SpecificServicesDialog(
                new ArrayList<Service>(
                        new HashSet<Service>(services)), entity);
        dialog.setVisible(true);
    }
}

